class Solution {
public:
    int deleteAndEarn(vector<int>& nums) {
        int m = nums.size();
        const int N = 1e4 + 1;
        if (m == 1)
            return nums[0];

        vector<int> vt(N, 0);
        for (auto x : nums)
        {
            vt[x] += x;
        }

        vector<int> f(N);
        vector<int> g(N);
        for (int i = 1; i < N; ++i)
        {
            f[i] = g[i - 1] + vt[i];
            g[i] = max(f[i - 1], g[i - 1]);
        }
        return max(f[N - 1], g[N - 1]);
    }
};